package com.atguigu.gulimall.order.web;

import com.atguigu.gulimall.order.exception.NoStockException;
import com.atguigu.gulimall.order.service.OrderService;
import com.atguigu.gulimall.order.service.OrderWebService;
import com.atguigu.gulimall.order.vo.ConfirmVo;
import com.atguigu.gulimall.order.vo.OrderSubmitResponseVo;
import com.atguigu.gulimall.order.vo.OrderSubmitVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

@Controller
public class OrderWeb {

    @Autowired
    OrderWebService webService;

    @Autowired
    OrderService orderService;

    @GetMapping("/pay.html")
    public String add(){
        return "pay";
    }

    @GetMapping("/toTrade")
    public String toTrade(Model model){
       ConfirmVo confirmVo = webService.getToRrade();
       model.addAttribute("confirmVo",confirmVo);
        return "toTrade";
    }

    /**
     * 提交订单
     */
    @PostMapping("sunmitOrder")
    public String submitOrder(OrderSubmitVo vo,Model model){
        OrderSubmitResponseVo responseVo = null;
        try {
            responseVo = orderService.submitOrder(vo);
        } catch (NoStockException e) {
            model.addAttribute("data",e.getMessage());
            return "toTrade";
        }
        return "pay.html";
    }
}
